package com.control.qct.businesssteward.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.GoodsStatisticAdapter;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.costom.MyXAxisValueFormatter;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.GoodsStatisticModel;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.DateUtil;
import com.control.qct.businesssteward.utils.ScreenUtil;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 商品统计表
 * Created by Administrator on 2017/12/26 0026.
 */

public class GoodsStatisticFragment extends BaseFragment {
    @BindView(R.id.bc_goods_statistic)
    BarChart mBcGoodsStatistic;
    @BindView(R.id.ll_goods_statistic)
    LinearLayout mLlGoodsStatistic;
    @BindView(R.id.rv_goods_statistical)
    RecyclerView mRvGoodsStatistical;
    private Map<String, String> mParams;
    private GoodsStatisticAdapter mGoodsStatisticAdapter;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_goods_statistic, container, false);
    }

    @Override
    protected void initView(View view) {
        activity.setTitle("商品统计表");
        mRvGoodsStatistical.setLayoutManager(new LinearLayoutManager(activity));
        mGoodsStatisticAdapter = new GoodsStatisticAdapter();
        mRvGoodsStatistical.setAdapter(mGoodsStatisticAdapter);
        //设置柱状图显示区域宽高
        mBcGoodsStatistic.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int) (ScreenUtil.getScreenHeightPix(activity) / 2.2)));
        StoreModel storeModel = (StoreModel) mSPUtils.getObject(mNetProtocol.getUserCode() + "_store");
        getProductStatistic(DateUtil.toTime6(System.currentTimeMillis()), storeModel == null ? "" : storeModel
                .getStoreId());
    }

    public void getProductStatistic(String date, String storeId) {
        mParams = new HashMap<>();
        mParams.put("date", date);
        mParams.put("storeId", storeId);
        mNetProtocol.getProductStatistic(activity, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                List<GoodsStatisticModel> goodsStatisticModels = (List<GoodsStatisticModel>) baseBean.getData();
                mLlGoodsStatistic.setVisibility(goodsStatisticModels.size() > 0 ? View.VISIBLE : View.GONE);
                mGoodsStatisticAdapter.setNewData(goodsStatisticModels);
                setBcGoodsStatistic(goodsStatisticModels);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    private void setBcGoodsStatistic(List<GoodsStatisticModel> data) {
        mBcGoodsStatistic.getDescription().setEnabled(false);
        mBcGoodsStatistic.setMaxVisibleValueCount(10);
        //是否可缩放
        mBcGoodsStatistic.setPinchZoom(false);
        //是否显示柱状图灰色背景图
        mBcGoodsStatistic.setDrawBarShadow(false);
        //是否启动灰色背景图 默认false
        mBcGoodsStatistic.setDrawGridBackground(false);


        //修改X轴元素内容对象
        IAxisValueFormatter xAxisFormatter = new MyXAxisValueFormatter(data);
        //获取X轴对象
        XAxis xAxis = mBcGoodsStatistic.getXAxis();
//        xAxis.setLabelRotationAngle(-60);
        //设置X轴显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //是否显示方格横线
        xAxis.setDrawGridLines(false);
        //间隔尺寸
        xAxis.setGranularity(1f);
        //设置显示元素格式及是否对齐柱状图
        xAxis.setLabelCount(data.size());
        //设置X轴元素显示内容
        xAxis.setValueFormatter(xAxisFormatter);

        //获取左侧Y轴对象
        YAxis leftAxis = mBcGoodsStatistic.getAxisLeft();
        leftAxis.setLabelCount(data.size());
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(20f);
        leftAxis.setAxisMinimum(0f); //

        YAxis rightAxis = mBcGoodsStatistic.getAxisRight();
        rightAxis.setEnabled(false);//禁用显示

        mBcGoodsStatistic.getAxisLeft().setDrawGridLines(false);
        mBcGoodsStatistic.animateY(800);
        mBcGoodsStatistic.getLegend().setEnabled(false);//禁用显示
        setData(data);
    }

    //设置数据
    private void setData(List<GoodsStatisticModel> list) {
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 1; i < list.size(); i++) {
            yVals1.add(new BarEntry(i, (float) list.get(i).getSaleTotal()));//添加Y轴元素
        }
        BarDataSet dataSet;
        if (mBcGoodsStatistic.getData() != null && mBcGoodsStatistic.getData().getDataSetCount() > 0) {
            dataSet = (BarDataSet) mBcGoodsStatistic.getData().getDataSetByIndex(0);
            dataSet.setValues(yVals1);
            mBcGoodsStatistic.getData().notifyDataChanged();
            mBcGoodsStatistic.notifyDataSetChanged();
        } else {
            dataSet = new BarDataSet(yVals1, "");
            //设置多彩 也可以单一颜色
            dataSet.setColors(Color.parseColor("#05C1AB"));
            dataSet.setDrawValues(false);

            ArrayList<IBarDataSet> dataSetList = new ArrayList<IBarDataSet>();
            dataSetList.add(dataSet);

            BarData data = new BarData(dataSetList);
            mBcGoodsStatistic.setData(data);
            mBcGoodsStatistic.setFitBars(true);
        }
        mBcGoodsStatistic.invalidate();
    }

}
